import { forEach, startsWith } from 'lodash';
import { Row, Col, Input, Button, message, Table, Tabs } from 'antd';
import styles from '../TableList.less';

const TabPane = Tabs.TabPane;

function generateScoreMap(phaseCode, scoreList) {
  var scoreMap = {};
  scoreMap.columns = []
  scoreMap.data = []
  const columns = scoreMap.columns
  const datas = scoreMap.data
  columns.push({
    title: '抽签号',
    key:'chouqianhao',
    className:styles.nopadding,
    width: 80,
    align:'right',
    dataIndex: 'posName',
    render:(value)=>(
      value === '轮空' ? <span style={{fontWeight:'bold', fontSize:18, color:'red'}}>轮空</span> : <span style={{fontWeight:'bold', fontSize:18}}>{value}</span>
    )
  })
  var count = 0
  debugger
  switch(scoreList.length){
    case 2:
      count=3
      break;
    case 4:
      count=4
      break;
    case 8:
      count =5
    break;
    case 16:
      count = 6;
      break;
  }
  for(var i=1;i<=count;i++){
    columns.push({
      title: 'line'+i,
      dataIndex:'line'+i,
      className:styles.nopadding,
      width: 80,
      render:(value) =>{
        if(value ==='fr') 
          return <div style={{height:'28px',borderStyle:'solid solid none none',borderWidth:'2px',borderColor:'#808080'}}>&nbsp;</div>
        else if(value ==='br') 
          return <div style={{height:'28px',borderStyle:'none solid solid none',borderWidth:'2px',borderColor:'#808080'}}>&nbsp;</div>
        else if(value ==='r')
          return <div style={{height:'28px',borderStyle:'none solid none none',borderWidth:'2px',borderColor:'#808080'}}>&nbsp;</div>
        else if(value ==='b')
          return <div style={{height:'28px',borderStyle:'none none solid none',borderWidth:'2px',borderColor:'#808080'}}>&nbsp;</div>
        else if(value ==='f')
          return <div style={{height:'28px',borderStyle:'solid none none none',borderWidth:'2px',borderColor:'#808080'}}>&nbsp;</div>
      }
    })
  }

  var index = 0;
  scoreList.forEach(element => {
    if (phaseCode && phaseCode !== element.phaseCode) {
      return
    }
    //第一行
    const userLine1 = {}
    datas.push(userLine1)
    //第二行
    const userLine2 = {}
    userLine2['line1'] = 'fr'
    datas.push(userLine2)
    //第三行
    const userLine3 = {}
    userLine3['line1'] = 'br'
    datas.push(userLine3)
    //第四行
    const userLine4 = {}
    datas.push(userLine4)
    if(element.gamePosName1 || element.gamePosName2){
      
      if(element.gamePosName1) {
        userLine1['posName']=element.gamePosName1 +' ' + generateAuth(element.athlethList1)
      } else {
        userLine1['posName']='轮空';
      }
      
      if(element.gamePosName2) {
        userLine3['posName']=element.gamePosName2+' '+generateAuth(element.athlethList2); 
      } else {
        userLine3['posName']='轮空';
      }
    }else{
      userLine1['posName']='待定';
      userLine3['posName']='待定';
    }

      switch(index){
        case 0:
          userLine3['line2'] = 'fr'
          userLine4['line2'] = 'r'
          break;
        case 1:
          userLine2['line2'] = 'br'
          userLine1['line2'] = 'r'
          if(count > 3){
            userLine1['line3'] = 'fr'
            userLine2['line3'] = 'r'
            userLine3['line3'] = 'r'
            userLine4['line3'] = 'r'
          }else if(count ===3){
            userLine1['line3'] = 'f'
          }else{
            userLine4['line3'] = 'b'
          }
          break;
        case 2:
          userLine3['line2'] = 'fr'
          userLine4['line2'] = 'r'
          if(count > 3){
            userLine1['line3'] = 'r'
            userLine2['line3'] = 'r'
            userLine3['line3'] = 'r'
            userLine4['line3'] = 'br'
          }
          if(count > 4){
            userLine1['line4'] = 'fr'
            userLine2['line4'] = 'r'
            userLine3['line4'] = 'r'
            userLine4['line4'] = 'r'
          }else{
            userLine1['line4'] = 'f'
          }
          break;
        case 3:
          userLine2['line2'] = 'br'
          userLine1['line2'] = 'r'
          if(count > 4){
            userLine1['line4'] = 'r'
            userLine2['line4'] = 'r'
            userLine3['line4'] = 'r'
            userLine4['line4'] = 'r'
          }
          break;
        case 4:
          userLine3['line2'] = 'fr'
          userLine4['line2'] = 'r'
          if(count > 4){
            userLine1['line4'] = 'r'
            userLine2['line4'] = 'r'
            userLine3['line4'] = 'r'
            userLine4['line4'] = 'r'
          }
          if(count > 5){
            userLine1['line5'] = 'fr'
            userLine2['line5'] = 'r'
            userLine3['line5'] = 'r'
            userLine4['line5'] = 'r'
          }else{
            userLine1['line5'] = 'b'
          }
          break;
        case 5:
          userLine2['line2'] = 'br'
          userLine1['line2'] = 'r'
          if(count > 3){
            userLine1['line3'] = 'fr'
            userLine2['line3'] = 'r'
            userLine3['line3'] = 'r'
            userLine4['line3'] = 'r'
          }
          if(count > 4){
            userLine1['line4'] = 'r'
            userLine2['line4'] = 'r'
            userLine3['line4'] = 'r'
            userLine4['line4'] = 'br'
          }
          if(count > 5){
            userLine1['line5'] = 'r'
            userLine2['line5'] = 'r'
            userLine3['line5'] = 'r'
            userLine4['line5'] = 'r'
          }
          break;
          case 6:
          userLine3['line2'] = 'fr'
          userLine4['line2'] = 'r'
          if(count > 3){
            userLine1['line3'] = 'r'
            userLine2['line3'] = 'r'
            userLine3['line3'] = 'r'
            userLine4['line3'] = 'br'
          }
          if(count > 5){
            userLine1['line5'] = 'r'
            userLine2['line5'] = 'r'
            userLine3['line5'] = 'r'
            userLine4['line5'] = 'r'
          }
          break;
        case 7:
          userLine2['line2'] = 'br'
          userLine1['line2'] = 'r'
          if(count > 5){
            userLine1['line5'] = 'r'
            userLine2['line5'] = 'r'
            userLine3['line5'] = 'r'
            userLine4['line5'] = 'r'
          }
          break;
          case 8:
          userLine3['line2'] = 'fr'
          userLine4['line2'] = 'r'
          if(count > 5){
            userLine1['line5'] = 'r'
            userLine2['line5'] = 'r'
            userLine3['line5'] = 'r'
            userLine4['line5'] = 'r'
          }
          if(count >= 6){
            userLine1['line6'] = 'b'
          }
          break;
        case 9:
          userLine2['line2'] = 'br'
          userLine1['line2'] = 'r'
          if(count > 3){
            userLine1['line3'] = 'fr'
            userLine2['line3'] = 'r'
            userLine3['line3'] = 'r'
            userLine4['line3'] = 'r'
          }
          if(count > 5){
            userLine1['line5'] = 'r'
            userLine2['line5'] = 'r'
            userLine3['line5'] = 'r'
            userLine4['line5'] = 'r'
          }
          break;
          case 10:
          userLine3['line2'] = 'fr'
          userLine4['line2'] = 'r'
          if(count > 3){
            userLine1['line3'] = 'r'
            userLine2['line3'] = 'r'
            userLine3['line3'] = 'r'
            userLine4['line3'] = 'br'
          }
          if(count > 4){
            userLine1['line4'] = 'fr'
            userLine2['line4'] = 'r'
            userLine3['line4'] = 'r'
            userLine4['line4'] = 'r'
          }
          if(count > 5){
            userLine1['line5'] = 'r'
            userLine2['line5'] = 'r'
            userLine3['line5'] = 'r'
            userLine4['line5'] = 'r'
          }
          break;
        case 11:
          userLine2['line2'] = 'br'
          userLine1['line2'] = 'r'
          if(count > 4){
            userLine1['line4'] = 'r'
            userLine2['line4'] = 'r'
            userLine3['line4'] = 'r'
            userLine4['line4'] = 'r'
          }
          if(count > 5){
            userLine1['line5'] = 'r'
            userLine2['line5'] = 'r'
            userLine3['line5'] = 'r'
            userLine4['line5'] = 'br'
          }
          break;
        case 12:
          userLine3['line2'] = 'fr'
          userLine4['line2'] = 'r'
          if(count > 4){
            userLine1['line4'] = 'r'
            userLine2['line4'] = 'r'
            userLine3['line4'] = 'r'
            userLine4['line4'] = 'r'
          }
          break;
        case 13:
          userLine2['line2'] = 'br'
          userLine1['line2'] = 'r'
          if(count > 3){
            userLine1['line3'] = 'fr'
            userLine2['line3'] = 'r'
            userLine3['line3'] = 'r'
            userLine4['line3'] = 'r'
          }
          if(count > 4){
            userLine1['line4'] = 'r'
            userLine2['line4'] = 'r'
            userLine3['line4'] = 'r'
            userLine4['line4'] = 'br'
          }
          break;
        case 14:
          userLine3['line2'] = 'fr'
          userLine4['line2'] = 'r'
          if(count > 3){
            userLine1['line3'] = 'r'
            userLine2['line3'] = 'r'
            userLine3['line3'] = 'r'
            userLine4['line3'] = 'br'
          }
          break;
        case 15:
          userLine2['line2'] = 'br'
          userLine1['line2'] = 'r'
          break;
      }
      
    index++;
  }); 
  return scoreMap;
};

function generateAuth(list){
  return list.map(value=> {return value.userName}).join(',')
}

export function generateTreeTable(phaseCode, zhixuChouqianList,handleChange) {
  const {tabList, list, curPage} = zhixuChouqianList
  const scoreMap = generateScoreMap(phaseCode, list);
  const tableDiv = <Table key='treeTable' pagination={false} size="small" showHeader={false} dataSource={scoreMap.data} columns={scoreMap.columns}/>
  if(tabList && tabList.length >1){
    return <Tabs tabPosition='left' size='small' onChange={handleChange}>{tabList.map(value=>(curPage === value.seq ? <TabPane tab={`第${value.name}区`} key={value.seq}>{tableDiv}</TabPane>:<TabPane tab={`第${value.name}区`} key={value.seq}></TabPane>))}</Tabs>
  }else{
    return tableDiv
  }

};
